var app = getApp();
var ApiUrl = app.globalData.ApiUrl;
var goods_detail = function (that) {
    
    var key = wx.getStorageSync('key');
  wx.showToast({
    title: "加载中",
    mask: true,
    icon: "loading",
    duration: 60000
  });
      wx.request({
        url: ApiUrl + '/index.php?act=goods&op=goods_detail',
        data: { "goods_id": that.data.goods_id, "key": key },
        method: 'GET',
        success: function (res) {
          wx.hideToast();
          
          if (res.data.code == 400) {
            app.showAlert(res.data.msg);
            return ;
            }
          var len = res.data.datas.goods_info.mobile_body.split('>');
         if(res.data.datas.voucher){
            wx.setStorageSync('voucher', res.data.datas.voucher);//代金券本地存储
         }
          var mobile_body = "";
           
          var f = 0;
          wx.getSystemInfo({
            success: function(res) {
             var w = res.screenWidth;
              for (var i = 0; i < len.length; i++) {                
                if(len[i]){
                  mobile_body += len[i]+" width='"+w+"px' />";
                }
              }
            },
          });
         
          
          var promo = '';
          switch (res.data.datas.goods_info.promotion_type) {
            case 'groupbuy':
              promo = '团购'; break;
            case 'xianshi':
              promo = '限时折扣'; break;
            case 'sole':
              promo = '手机专享'; break;
          }
          //商品规格格式化数据
         
          
          if (res.data.datas.goods_info.spec_name) {
            var goods_specs = new Array();
            
            for (var v in res.data.datas.goods_info.spec_name) {
            
              
              var resa = res.data.datas.goods_info.spec_value;
              if (resa) { 
                  var specs_value = new Array();                  
                  for (let i in resa) {
                      if (i == v) {  
                          for (var j in resa[i]) {                             
                            specs_value.push({'specs_value_id':j,'specs_value_name':resa[i][j]});
                          } 
                    }      
                  }                 
              }
              goods_specs.push({'goods_spec_id': v, 'goods_spec_name': res.data.datas.goods_info.spec_name[v],'goods_spec_value': specs_value});
                 
            }  
          }  
          var cart = "";
          var BuycartLanguage = "";
          var addCartLanguage = "";
          switch (res.data.datas.goods_info.goods_state) {
            case '1':
              cart = "showCart";
              BuycartLanguage = "立即购买";
              addCartLanguage = "加入购物车"; break;
            case '0':
              cart = "";
              BuycartLanguage = "商品已下架或不存在";
              addCartLanguage = "商品已下架或不存在"; break;
            case '10':
              cart = "";
              BuycartLanguage = "商品已下架或不存在";
              addCartLanguage = "商品已下架或不存在"; break;
          }
          var cart_count = wx.getStorageSync('cart_count');
         
          var newDate = new Date();
          newDate.setTime(res.data.datas.goods_info.presell_deliverdate * 1000);
          res.data.datas.goods_info.presell_deliverdate = newDate.toLocaleDateString();
          var voucher = res.data.datas.voucher ? res.data.datas.voucher:"";

          that.setData({
            cart: cart,
            key: key?key:"",
            cart_count: cart_count ? cart_count : 0,
            BuycartLanguage: BuycartLanguage,
            addCartLanguage: addCartLanguage,
            goods_info: res.data.datas.goods_info,
            cartNum: 1,
            promo: promo,
            goods_hair_info: res.data.datas.goods_hair_info,
            goods_image: res.data.datas.goods_image.split(","),
            goods_commend_list: res.data.datas.goods_commend_list,
            mansong_info: res.data.datas.mansong_info,
            gift_array: res.data.datas.gift_array,
            // goods_map_spec: goods_specs,
            spec_image: res.data.datas.spec_image,
            spec_list: res.data.datas.spec_list,
            store_info: res.data.datas.store_info,
            goods_eval_list: res.data.datas.goods_eval_list,
            goods_evaluate_info: res.data.datas.goods_evaluate_info,
            // goods_bundling_list: res.data.datas.goods_bundling_list,
            // goods_bundling_list_length: res.data.datas.goods_bundling_list_length,
            // b_goods_list0_length: res.data.datas.b_goods_list0_length,
            // b_goods_list0: res.data.datas.b_goods_list0,
            // b_goods_list1: res.data.datas.b_goods_list1,
            // b_goods_list1_length: res.data.datas.b_goods_list1_length,
            // b_goods_list2: res.data.datas.b_goods_list2,
            // b_goods_list2_length: res.data.datas.b_goods_list2_length,
            // b_goods_list3: res.data.datas.b_goods_list3,
            // b_goods_list3_length: res.data.datas.b_goods_list3_length,
            // b_goods_list4: res.data.datas.b_goods_list4,
            // b_goods_list4_lengt: res.data.datas.b_goods_list4_length,
            // b_goods_list4: res.data.datas.b_goods_list4,
            // b_goods_list4_lengt: res.data.datas.b_goods_list4_length,
            mobile_body: mobile_body,
            voucher: voucher
          });
          
        },
      });
     
      
}
Page({
  data:{
    biaosblen:false,
    flag:''//分享码传入,分销员的推广码

  }, 

  onLoad: function (options) {
    var that = this;
    var flag = '';
    if (options.flag) flag = options.flag;
    that.setData({
        goods_id:options.goods_id,
        flag: flag
    });
    goods_detail(that);

  },
  getDetail:function(options){
      
      var that = this;
      that.setData({
        goods_id: options.currentTarget.dataset.goods_id
      });
    goods_detail(that);  
  },
  plus:function(e){
    
      var that = this;
      var num = e.currentTarget.dataset.num +1 ;
      that.setData({
        cartNum:num
      });
  },
  minus:function(e){
    var that = this;
    if (e.currentTarget.dataset.num>1){
      var num = e.currentTarget.dataset.num - 1;
    }else{
      var num = e.currentTarget.dataset.num;
    }
    
    that.setData({
      cartNum: num
    });
  },
  imageLoad:function(e){
     
    var that = this;
    var height = parseFloat(e.detail.height);
    var width = parseFloat(e.detail.width);
       wx.getSystemInfo({
         success: function(res) {
           var screenWidth = res.screenWidth;
          var newwidth = width/screenWidth;
          var newheight = height /newwidth;
          if(e.target.dataset.type=='swiper'){
            that.setData({
              swiperW: width+'px',
              swiperH:height+'px'
            });
          }else{
          that.setData({
            height: newheight,
            width: screenWidth
          });
          }
         },
       })
      
  },
 
  buyNow:function(e){     
      var that = this;
      var buynum  = this.data.cartNum;
      var goods_id = e.currentTarget.dataset.goods_id;
      var key = wx.getStorageSync('key');
      if (!key){          
        wx.navigateTo({
          url: '/pages/login/login',
        })

      }else{
        wx.navigateTo({
          url: '/pages/order/order?goodsId=' + goods_id + '&buynum=' + buynum + "&type=only" ,
        })
        // wx.navigateTo({
        //   url: '/pages/buy_step1/buy_step1?goodsId=' + goods_id + '&buynum=' + buynum + "&key=" + key,
        // })
      }
  },
  addCarTab:function (e){        
        var that = this;
        var goods_id = e.currentTarget.dataset.goodsid;
        var key = wx.getStorageSync('key');       
        var quantity = this.data.cartNum;
        if (!key) {
        
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }else{
              var data = '';
             
              var cart_count = wx.getStorageSync('cart_count');
              wx.request({
                  url:ApiUrl+"/index.php?act=member_cart&op=cart_add",
                  data:{
                    'goods_id': goods_id,
                    'quantity':quantity,
                    'key'     :key,
                    'flag'    :that.data.flag
                  },  
                  header: {
                    'content-type': 'application/x-www-form-urlencoded'
                  },                        
                  method:"POST",
                  success: function (result) { 
                
                    if (result.data.code==200){                       
                        wx.setStorageSync("cart_count", that.data.cart_count);              
//                         wx.showModal({
//                           content: "已加入购物车，去结算吗？",
//                           confirmText: "确定",
//                           cancelText: "取消",
//                           cancelColor: '#0080FF',
//                           confirmColor: '#0080FF',
//                           success: function (res) {                               
//                             if (res.confirm) {
//                               wx.navigateTo({url: "/pages/carts_list/carts_list",})
//                             } 
//                           },
//                           fail:function(res){
// //console.log("取消");
//                           },
//                           complete:function(){
//                             that.setData({
//                               cart_count: cart_count + 1
//                             });
//                           }
//                         })
                        //biaosblen
                        that.setData({
                          biaosblen: true,
                        });
                      }                         
                  }
              })
          }
   },
 
  closeTab:function(){
        var that = this;
      that.setData({
        "classTab":'down',
        "ishide"  :'block',
        "buttombuy": 'none'
      });
  },
  showCart:function(){
      var that = this;
      that.setData({
        "classTab":'up',
        "ishide"  :'none',
        "buttombuy":'block'
      });
  },
  changeSpec:function(e){
      
      var that = this;
      var specs_value_id = e.target.dataset.specs_value_id;
     
      that.setData({
        goods_id: that.data.spec_list[specs_value_id]
      }); 
    goods_detail(that);

  },
  onShareAppMessage: function (ops) {
        console.log(ops,3333)
        var that = this;
        //转发事件来源。
        //button：页面内转发按钮；
        //menu：右上角转发菜单

        if (ops.from === 'button') {
            var title = ops.target.dataset.title;
        };

      var user = wx.getStorageSync('user_info');
      if(user.flag=='')return false;
        return {
            title: title, //转发的标题。当前小程序名称
            path: '/pages/product_detail/product_detail?goods_id='+that.data.goods_id+'&flag='+user.flag, //转发的路径
            imageUrl: '',//自定义图片路径 支持PNG及JPG。显示图片长宽比是 5:4。
        }
    },
  handleContact(e) {
//console.log(e.path)
//console.log(e.query)
  }
})